#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int cmp(const void* p1, const void* p2)
{
    return *(int*)p1 - *(int*)p2;
}
void nextPermutation(int* nums, int numsSize) {
    int right = numsSize - 1, left = right - 1;
    while (left >= 0)
    {
        if (nums[right] > nums[left])
        {
            right = numsSize - 1;
            while (nums[right] <= nums[left])
            {
                right--;
            }
            int tmp = nums[left];
            nums[left] = nums[right];
            nums[right] = tmp;
            if (right == numsSize - 1 && left == right - 1) {
                return;
            }
            else {
                qsort(&nums[left + 1], numsSize-1 - left, sizeof(int), cmp);
                return;
            }
        }
        else {
            left--;
            right--;
        }
    }
    if (left < 0) {
        qsort(nums, numsSize, sizeof(int), cmp);
    }
}
int main()
{
	int arr[] = { 1,5,1 };
	int sz = sizeof(arr) / sizeof(arr[0]);
    nextPermutation(arr, sz);
	return 0;
}